package com.nuoniu.sibanyun.service.crm.call.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallMode;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallVoice;
import com.nuoniu.sibanyun.entity.crm.call.dto.CrmCallModeSearchDto;
import com.nuoniu.sibanyun.mapper.crm.call.CrmCallModeMapper;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallModeService;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallTaskService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 客户管理-外呼管理-快呼模式 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2022-02-28
 */
@Service
public class CrmCallModeServiceImpl extends ServiceImpl<CrmCallModeMapper, CrmCallMode> implements ICrmCallModeService {

    @Autowired
    private ICrmCallTaskService taskService;

    @Override
    public Page<CrmCallMode> pageList(CrmCallModeSearchDto crmCallModeSearchDto) {
        UserInfo userInfo = UserThreadLocal.get();
        Page<CrmCallMode> page = crmCallModeSearchDto.getPage();
        QueryWrapper<CrmCallMode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        String modeName = crmCallModeSearchDto.getModeName();
        Integer callType = crmCallModeSearchDto.getCallType();
        if(StringUtils.isNotBlank(modeName)){
            queryWrapper.like("mode_name",modeName);
        }
        if( null != callType){
            queryWrapper.eq("call_type",callType);
        }
        return page(page,queryWrapper);
    }

    @Override
    public boolean checkName(String name, Long id) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<CrmCallMode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("voice_name",name);
        if(null != id){
            queryWrapper.ne("id",id);
        }
        return count(queryWrapper) > 0;
    }

    @Override
    public void saveMode(CrmCallMode crmCallMode) {
        boolean isHaveName = this.checkName(crmCallMode.getModeName(), crmCallMode.getId());
        if(isHaveName)throw new NuoNiuException("名称不能重复");
        this.saveOrUpdate(crmCallMode);
    }

    @Override
    public void deleteById(Long id) {
        if(null == id || id < 1)throw new NuoNiuException("参数错误");
        UserInfo userInfo = UserThreadLocal.get();
        CrmCallMode oldCrmCallMode = getById(id);
        if(null == oldCrmCallMode)throw new NuoNiuException("外呼模式记录不存在");
        boolean isUseMode = taskService.checkMode(id);
        if(isUseMode)throw new NuoNiuException("外呼模式已被使用，不能删除");
        QueryWrapper<CrmCallMode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("id",id);
        this.remove(queryWrapper);
    }

    @Override
    public boolean checkVoice(Long voiceId) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<CrmCallMode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("voice_id",voiceId);
        return count(queryWrapper) > 0;
    }
}
